package com.bawei.module_moto_circle.ui.search

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.SearchEvent
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.bawei.lib_common.flow_eventbus.LoginEvent
import com.bawei.lib_common.flow_eventbus.post.postEvent
import com.bawei.module_moto_circle.R
import com.bawei.module_moto_circle.databinding.FragmentSearchSuggestionsBinding
import com.bawei.module_moto_circle.model.SearchKeywordModelItem
import com.bawei.module_moto_circle.ui.search.adapter.SearchSuggestionAdapter
import com.bawei.myclub.base.mvvm.view.BaseFragment


class SearchSuggestionsFragment :
    BaseFragment<SearchViewModel, FragmentSearchSuggestionsBinding>() {

    override val mLayoutResId: Int
        get() = R.layout.fragment_search_suggestions
    override val mViewModel: SearchViewModel by activityViewModels()
    private var mOnlyTopic: Boolean = false
    private val mAdapter by lazy {
        SearchSuggestionAdapter {
            postEvent(SearchSuggestionEvent(it))
        }
    }

    companion object {
        val TAG: String = SearchSuggestionsFragment::class.java.simpleName
        private const val ONLY_TOPIC = "only_topic"
        fun newInstance(onlyTopic: Boolean): SearchSuggestionsFragment {
            val args = Bundle()
            args.putBoolean(ONLY_TOPIC, onlyTopic)
            val fragment = SearchSuggestionsFragment()
            fragment.arguments = args
            return fragment
        }
    }

    /**请求状态*/
    fun onLoading() {
        mBinding.tvHint.visibility = View.VISIBLE
        mBinding.lEmpty.visibility = View.GONE
        mBinding.tvHint.text = "搜索中..."
    }

    fun onSuccess(keyword: String) {
        mAdapter.setHighlightWord(keyword)
        mBinding.tvHint.visibility = View.GONE
        mBinding.lEmpty.visibility = View.GONE
        mBinding.tvHint.text = ""
    }

    fun onError(msg: String) {
        mBinding.tvHint.visibility = View.GONE
        mBinding.lEmpty.visibility = View.VISIBLE
        mBinding.tvErrorViewHint.text = msg
    }

    override fun initView() {
        mOnlyTopic = arguments?.getBoolean(ONLY_TOPIC) ?: false
        initAdapter()
    }

    private fun initAdapter() {
        mBinding.rvSuggestion.adapter = mAdapter
        mBinding.rvSuggestion.layoutManager = LinearLayoutManager(requireContext())
    }

    fun setNewData(list: List<SearchKeywordModelItem>) {
        if (mOnlyTopic) {
            mAdapter.setData(list.filter { it.socialType == 1 })
        } else {
            mAdapter.setData(list)
        }

    }

    fun clearData() {
        mAdapter.clearData()
    }



}